package com.ruoyi.kesheSys.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.kesheSys.domain.vo.PopulationRegionVo;
import com.ruoyi.kesheSys.to.BusinessTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.kesheSys.domain.PopulationInfo;
import com.ruoyi.kesheSys.mapper.PopulationRegionMapper;
import com.ruoyi.kesheSys.domain.PopulationRegion;
import com.ruoyi.kesheSys.service.IPopulationRegionService;

/**
 * 人口区域信息Service业务层处理
 * 
 * @author keshe
 * @date 2024-12-22
 */
@Service
public class PopulationRegionServiceImpl implements IPopulationRegionService 
{
    @Autowired
    private PopulationRegionMapper populationRegionMapper;

    /**
     * 查询人口区域信息
     * 
     * @param regionId 人口区域信息主键
     * @return 人口区域信息
     */
    @Override
    public PopulationRegion selectPopulationRegionByRegionId(Long regionId)
    {
        return populationRegionMapper.selectPopulationRegionByRegionId(regionId);
    }

    /**
     * 查询人口区域信息列表
     * 
     * @param populationRegion 人口区域信息
     * @return 人口区域信息
     */
    @Override
    public List<PopulationRegion> selectPopulationRegionList(PopulationRegion populationRegion)
    {
        return populationRegionMapper.selectPopulationRegionList(populationRegion);
    }

    /**
     * 新增人口区域信息
     * 
     * @param populationRegion 人口区域信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPopulationRegion(PopulationRegion populationRegion)
    {
        populationRegion.setCreateTime(DateUtils.getNowDate());
        int rows = populationRegionMapper.insertPopulationRegion(populationRegion);
        insertPopulationInfo(populationRegion);
        return rows;
    }

    /**
     * 修改人口区域信息
     * 
     * @param populationRegion 人口区域信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePopulationRegion(PopulationRegion populationRegion)
    {
        populationRegion.setUpdateTime(DateUtils.getNowDate());
        populationRegionMapper.deletePopulationInfoByRegionId(populationRegion.getRegionId());
        insertPopulationInfo(populationRegion);
        return populationRegionMapper.updatePopulationRegion(populationRegion);
    }

    /**
     * 批量删除人口区域信息
     * 
     * @param regionIds 需要删除的人口区域信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePopulationRegionByRegionIds(Long[] regionIds)
    {
        populationRegionMapper.deletePopulationInfoByRegionIds(regionIds);
        return populationRegionMapper.deletePopulationRegionByRegionIds(regionIds);
    }

    /**
     * 删除人口区域信息信息
     * 
     * @param regionId 人口区域信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePopulationRegionByRegionId(Long regionId)
    {
        populationRegionMapper.deletePopulationInfoByRegionId(regionId);
        return populationRegionMapper.deletePopulationRegionByRegionId(regionId);
    }


    /**
     * 查询每个区域的人口统计信息
     *
     *@param  populationRegion 人口区域信息
     * @return populationRegionVo 人口区域信息集合
     */


    @Override
    public List<PopulationRegionVo> selectPopulationRegionListVo(PopulationRegion populationRegion) {
        return populationRegionMapper.selectPopulationRegionListVo(populationRegion);
    }


    /**
     * 查询各区域人口比重
     *
     * @return 各区域人口统计数据列表

    @Override
    public List<BusinessTo> selectPopulationPercentageByRegion() {
        return populationRegionMapper.selectPopulationPercentageByRegion();
    }
 */



    /**
     * 新增人口基本信息信息
     * 
     * @param populationRegion 人口区域信息对象
     */
    public void insertPopulationInfo(PopulationRegion populationRegion)
    {
        List<PopulationInfo> populationInfoList = populationRegion.getPopulationInfoList();
        Long regionId = populationRegion.getRegionId();
        if (StringUtils.isNotNull(populationInfoList))
        {
            List<PopulationInfo> list = new ArrayList<PopulationInfo>();
            for (PopulationInfo populationInfo : populationInfoList)
            {
                populationInfo.setRegionId(regionId);
                list.add(populationInfo);
            }
            if (list.size() > 0)
            {
                populationRegionMapper.batchPopulationInfo(list);
            }
        }
    }
}
